package com.syq.example.proxy.cglibProxy;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;


public class CglibInterceptor implements MethodInterceptor {

	private Enhancer enhancer = new Enhancer();


	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("拦截之前ǰ");
		Object o = proxy.invokeSuper(obj, args);
		System.out.println("拦截之后");
		return o;
	}


	public Object newProxyInstance(Class<?> c) {

		enhancer.setSuperclass(c);//根据class得到父类

		enhancer.setCallback(this);//设置回调

		return enhancer.create();//创建对象
	}
}